<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>註冊reg.jsp</title>
<%	
	
	String input_uid=request.getParameter("uid");
	String input_unm=request.getParameter("unm");
	String input_upw1=request.getParameter("upw1");
	String input_upw2=request.getParameter("upw2");
	String input_uem=request.getParameter("uem");
	
	String emsg_uid=""; 
    String emsg_unm="";
    String emsg_upw1="";
    String emsg_upw2="";
    String emsg_uem="";
    
    //uid
	if	(input_uid!=null)
	{	if 	(4<=input_uid.length())
		{	if	(input_uid.length()<=20) emsg_uid="";
			else emsg_uid="Error: uid length>20";
		}else emsg_uid="uid length <4 ! ";
	}else 
	{  
		input_uid="";
		emsg_uid="";
	}
	
	
	//unm
		if	(input_unm!=null)
		{	if 	(4<=input_unm.length())
			{	if	(input_unm.length()<=20) emsg_unm="";
				else emsg_unm="Error: uid length>20";
			}else emsg_unm="unm length <4 ! ";
		}else 
		{  
			input_unm="";
			emsg_unm="";
		}
		
		//upw1
		if	(input_upw1!=null)
		{	if 	(4<=input_upw1.length())
			{	if	(input_upw1.length()<=20) emsg_upw1="";
				else emsg_upw1="Error: upw1 length>20";
			}else emsg_upw1="upw1 length <4 ! ";
		}else 
		{  
			input_upw1="";
			emsg_upw1="";
		}
		
		//upw2
		if	(input_upw1.equals(input_upw2)) emsg_upw2="";
		else 
		{  	input_upw2="";
			emsg_upw2="pw2 unmatched with pw1!";
		}
		
		//uem
		if	(input_uem!=null)
		{	int index_at=input_uem.indexOf("@");
			if 	(index_at>0)
			{	String str_before_at=input_uem.substring(0,index_at);
				if (str_before_at.length()>0) emsg_uem="";
				else emsg_uem="email format error-1.";
			}else emsg_uem="email format error-2(no @).";			
		}else
		{  	input_uem="";
			emsg_uem="";
		}
		String error=emsg_uid+emsg_unm+emsg_upw1+emsg_upw2+emsg_uem;
		if 	(error.length()==0)
		{	String mysql_url="j.nnpy.org/102";
			String mysql_dbac="jstu2";
			String mysql_dbpw="abc123";
			String mysql_connstr="jdbc:mysql://"+mysql_url;
			String mysql_cmd="select * from users";
			
			Connection conn=null;
			Statement stmt=null;
			ResultSet rs=null;
			
			try{
				DriverManager.registerDriver(new com.mysql.jdbc.Driver());
				conn=DriverManager.getConnection(mysql_connstr, mysql_dbac, mysql_dbpw);
			}catch( SQLException e)
			{e.printStackTrace();}
			stmt=conn.createStatement();
			rs=stmt.executeQuery(mysql_cmd);
			
		
		}
		else emsg_uem="thanks!";
		
		
		
%>       
</head>
<body>
<form action="" >
 	   帳號:<input name="uid" type="text" value="<%=input_uid %>"> (4~20中英文字)<span style="color: #ff0000;"> <%= emsg_uid%></span><br/>
 	    暱稱:<input  name="unm" type="text" value="<%=input_unm %>"> (4~20英文字,不能?中文)<span style="color: #ff0000;"> <%= emsg_unm%></span><br/>
 	   密碼1:<input name="upw1" type="text" value="<%=input_upw1 %>"> (4~20英?字)<span style="color: #ff0000;"> <%= emsg_upw1%></span><br/>
  	   密碼2:<input name="upw2" type="password" value="<%=input_upw2 %>"> (和密?1一?)<span style="color: #ff0000;"> <%= emsg_upw2%></span><br/>
 	 E-mail:<input name="uem" type="text" value="<%=input_uem %>">(4~50英文字含@)<span style="color: #ff0000;"> <%=emsg_uem%></span><br/>
 	 <input name="reg_submit" type="submit" value="註冊"/>
 </form>

</body>
</html>